package com.qingluan.dd;

import java.io.IOException;

import org.json.JSONException;

import com.qingluan.DataFormat.PersonTag;
import com.qingluan.DataFormat.PersonTag.ChangeListener;
import com.qingluan.Database.Personal_Info_database;
import com.qingluan.arguments.ARGUMENTS;
import com.qingluan.widget.BitmapHelper;
import com.qingluan.widget.SelectPictureActivity;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

public class PersonInfoActivity extends Activity {
	private ImageView own_pic;
	private EditText name,describe;
	private final static int  REQUEST_CODE = 1;
	private BitmapHelper bitmapHelper ;
	//context argu area
	private Context context;
	private static String tag = PersonInfoActivity.class.getName(); 
	//token area
	private String token_id;
	private String token_key;
	private String uid;
	private String OldName ;
	
	
	//callback intent code 
	public static int CHANGED_REFRESH = 2;
	private boolean ifChangePhoto = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_person_info);
		
		context = getApplicationContext();
		uid = getIntent().getExtras().getString("uid");
		token_id = getIntent().getExtras().getString("token_id");
		token_key = getIntent().getExtras().getString("token_key");
		
		own_pic = (ImageView)findViewById(R.id.own_pic);
		
		name = (EditText) findViewById(R.id.own_name);
	
		describe = (EditText) findViewById(R.id.own_describe);
		
		bitmapHelper = new BitmapHelper(this);

		OldName = name.getText().toString();
		
		try {
			load(own_pic);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		own_pic.setOnClickListener(new ChangePhoto());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.person_info, menu);
		return true;
	}
	
	public void load(ImageView v) throws IOException{
		String uid = getIntent().getExtras().getString("uid");
		bitmapHelper.BitmapLoader(uid, v);
	}
	
	class ChangePhoto implements OnClickListener {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			ifChangePhoto = true;
			Intent intent = new Intent(PersonInfoActivity.this,SelectPictureActivity.class);
			intent.putExtra("uid", uid);
			PersonInfoActivity.this.startActivityForResult(intent, REQUEST_CODE);
		}
		
	}

	@Override
	public void onActivityResult(int requestCode,int resultCode ,Intent data){
		if (requestCode == REQUEST_CODE && data != null){
			Bundle bitmap_data = null;
			if ((bitmap_data = data.getExtras()) != null){
				 Bundle realData =  bitmap_data.getBundle("data");
				 try{
					 Bitmap iv = (Bitmap) realData.get("data");
					 own_pic.setImageBitmap(iv);
				 }catch (NullPointerException e){
					 Log.d("I_GET","not take photo mode");
				 }
			}
			Uri select = data.getData();
			own_pic.setImageURI(select);
		}
	}
	
	public  void change_person_info (View view){
		//post  your personal info to server

		final String new_name = name.getText().toString();
		final String new_tag = name.getText().toString();
		final String new_desc = describe.getText().toString();
		

		
		final String email = new Personal_Info_database(context).searchEmail(OldName);
		
		try {
			new PersonTag(context,uid).ChangeOwnInfo(PersonTag.CHANGE_NAME, new_name,token_id,token_key, new ChangeListener(){

				@Override
				public void result(String info) {
					// TODO Auto-generated method stub
					if (info.equals("ok")){
						ContentValues values = new ContentValues();
						
						values.put("name", new_name);
						
						new Personal_Info_database(context).update(values, uid);
						Log.d(tag, new_name);
//						Toast.makeText(context, "Update name ok", Toast.LENGTH_SHORT).show();
						
					}
				}
				
			});
			
			new PersonTag(context,uid).ChangeOwnInfo(PersonTag.CHANGE_DESCRIBE, new_desc, token_id,token_key, new ChangeListener(){

				@Override
				public void result(String info) {
					// TODO Auto-generated method stub
					if (info.equals("ok")){
						ContentValues values = new ContentValues();
						
						
						values.put("describe",new_desc);
						
						new Personal_Info_database(context).update(values, uid);
						
						Toast.makeText(context, "Update describe ok", Toast.LENGTH_SHORT).show();
						
					}
				}
				
			});
			
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		
		
		
		Intent intent_callback = new Intent();
		intent_callback.putExtra("change", "ok");
		setResult(CHANGED_REFRESH, intent_callback);
		finish();
	}
}
